/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.editor;

import java.util.List;

import org.eclipse.emf.common.util.Enumerator;
import org.eclipse.gef.palette.CombinedTemplateCreationEntry;
import org.eclipse.gef.palette.ConnectionCreationToolEntry;
import org.eclipse.gef.palette.CreationToolEntry;
import org.eclipse.gef.requests.CreationFactory;
import org.eclipse.gef.requests.SimpleFactory;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.extensionpoints.palette.Button;
import com.cea.papyrus.core.pseudoelement.UMLElementFactory;
import com.cea.papyrus.core.utils.CreationToolEntryManager;

/**
 * Utility class to manage CreationToolEntry
 * In Papyrus, it is forbidden to write new CreationToolEntry(...)
 * 
 * @author Yann Tanguy
 * @author Patrick Tessier
 */
public class CreationEntryFactory {
	
	/**
	 * 
	 */
	private CreationToolEntryManager creationToolEntryManager =new CreationToolEntryManager(); 

	/**
	 * Creates a new component ready to be added in the palette
	 * Used to create elaborate uml types (ex: with stereotype)
	 * from a user defined PaletteMenu extension.
	 * 
	 * @param presentation 
	 * @param kind 
	 * @param label 
	 * @param tooltip 
	 * @param clazz 
	 * @param iconDescriptor 
	 * @param stereotypes 
	 * 
	 * @return 
	 * 
	 * @deprecated this method will become private
	 */
	@Deprecated
	public static CombinedTemplateCreationEntry createNodeCreationEntry (
					String label
					, String tooltip
					, Class clazz
					, String kind
					, List<String> stereotypes
					, ImageDescriptor iconDescriptor
					, String presentation)
	{	
		CombinedTemplateCreationEntry component = null;

		ImageDescriptor iconDesc;
		if (iconDescriptor == null) {
			Image icon = PapyrusPlugin.UML_LABEL_PROVIDER.getImage(clazz, kind);
			iconDesc = ImageDescriptor.createFromImage(icon);
		} else {
			iconDesc = iconDescriptor;
		}

		component = new CombinedTemplateCreationEntry(
				label
				, tooltip
				, clazz
				, new UMLElementFactory(clazz, kind, stereotypes, presentation)
				, iconDesc
				, iconDesc);

		return component;
	}
	
	/**
	 * /**
	 * Creates a new component ready to be added in the palette
	 * Used to create elaborate uml types (ex: with stereotype)
	 * from a user defined PaletteMenu extension.
	 * 
	 * @param presentation 
	 * @param kind 
	 * @param label 
	 * @param tooltip 
	 * @param clazz 
	 * @param iconDescriptor 
	 * @param stereotypes 
	 * 
	 * @return 
	 * 
	 * @deprecated this method will become private
	 */
	@Deprecated
	public static ConnectionCreationToolEntry createEdgeCreationEntry (
					String label
					, String tooltip
					, Class clazz
					, String kind
					, List<String> stereotypes
					, ImageDescriptor iconDescriptor
					, String presentation)
	{	
		ConnectionCreationToolEntry component = null;

		ImageDescriptor iconDesc;
		if (iconDescriptor == null) {
			Image icon = PapyrusPlugin.UML_LABEL_PROVIDER.getImage(clazz, kind);
			iconDesc = ImageDescriptor.createFromImage(icon);
		} else {
			iconDesc = iconDescriptor;
		}

		component = new ConnectionCreationToolEntry(
				label
				, tooltip
				, new UMLElementFactory(clazz, kind, stereotypes, presentation)
				, iconDesc
				, iconDesc);

		return component;
	}
	
	/**
	 * Creates a new component ready to be added in the palette
	 * Used to create elaborate uml types (ex: with stereotype)
	 * from a user defined Button in PaletteMenu extension.
	 * 
	 * @param button 
	 * 
	 * @return 
	 * 
	 * @deprecated this method will become private
	 */
	@Deprecated
	public static CombinedTemplateCreationEntry createNodeCreationEntry (Button button)
	{	
		Class clazz = button.getUMLType();
		String kind = button.getUMLTypeKind();
		
		CombinedTemplateCreationEntry component = null;

		ImageDescriptor iconDesc = button.getIconDescriptor();
		if (iconDesc == null) {
			Image icon = PapyrusPlugin.UML_LABEL_PROVIDER.getImage(clazz, kind);
			iconDesc = ImageDescriptor.createFromImage(icon);
		}

		component = new CombinedTemplateCreationEntry(
				button.getLabel()
				, button.getTooltip()
				, clazz
				, new UMLElementFactory(button)
				, iconDesc
				, iconDesc);

		return component;
	}
	
	/**
	 * Creates a new component ready to be added in the palette.
	 * 
	 * @param typeName 	 * @param kind 
	 * @param label 
	 * @param tooltip 
	 * @param clazz 
	 * @param name 	 * 
	 * @return 
	 * 
	 * @deprecated this method will become private
	 */
	@Deprecated
	public static CombinedTemplateCreationEntry createCombinedTemplateCreationEntry (String label, String tooltip, Class clazz, Enumerator kind) {
		CombinedTemplateCreationEntry component = null;

		Image icon = PapyrusPlugin.UML_LABEL_PROVIDER.getImage(clazz, kind);
		ImageDescriptor iconDesc = ImageDescriptor.createFromImage(icon);

		component = new CombinedTemplateCreationEntry(
				label
				, tooltip
				, clazz
				, new SimpleFactory(clazz)
				, iconDesc
				, iconDesc);

		return component;
	}

	/**
	 * Creates a new ConnectionCreationToolEntry ready to be added in the palette.
	 * 
	 * @param typeName 	 * @param kind 
	 * @param label 
	 * @param tooltip 
	 * @param clazz 
	 * @param name 	 * 
	 * @return 
	 * 
	 * @deprecated this method will become private
	 */
	@Deprecated
	public static ConnectionCreationToolEntry createConnectionCreationToolEntry (String label, String tooltip, Class clazz, Enumerator kind) {
		ConnectionCreationToolEntry component = null;

		Image icon = PapyrusPlugin.UML_LABEL_PROVIDER.getImage(clazz, kind);
		ImageDescriptor iconDesc = ImageDescriptor.createFromImage(icon);

		final Class type = clazz;

		component 
		= new ConnectionCreationToolEntry(
				label
				, tooltip
				, new CreationFactory() {
					public Object getNewObject() { return null; }
					public Object getObjectType() { return type; }
				} 
				, iconDesc
				, iconDesc);


		return component;
	}

	/**
	 * 
	 * 
	 * @param returnTypeName 	 * @param kind 
	 * @param label 
	 * @param tooltip 
	 * @param clazz 
	 * @param name 	 * 
	 * @return 
	 * 
	 * @deprecated this method will become private
	 */
	@Deprecated
	public static CreationToolEntry createCreationToolEntry (String label, String tooltip, Class clazz, Enumerator kind) {
		CreationToolEntry component = null;

		Image icon = PapyrusPlugin.UML_LABEL_PROVIDER.getImage(clazz, kind);
		ImageDescriptor iconDesc = ImageDescriptor.createFromImage(icon);

		component 
		= new CreationToolEntry(
				label
				, tooltip
				, new SimpleFactory(clazz)
				, iconDesc 
				, iconDesc);

		return component;
	}
	
	/**
	 * to obtain the creation tool entry from the symbolic name.
	 * 
	 * @param key sysmbolic name
	 * @param kind 
	 * @param label 
	 * @param tooltip 
	 * @param clazz 
	 * 
	 * @return the creationtool Entry
	 */
	public CreationToolEntry getCombinedTemplateCreationToolEntry(String label, String tooltip, Class clazz, Enumerator kind){
		String symbolicName=label+tooltip+clazz+kind; 
		CreationToolEntry result= creationToolEntryManager.get(symbolicName);
		if(result==null){
			creationToolEntryManager.put(symbolicName, createCombinedTemplateCreationEntry(label, tooltip, clazz, kind));
			result= creationToolEntryManager.get(symbolicName);
		}
		
		return result;
	}
	
	/**
	 * to obtain the creation tool entry from the symbolic name.
	 * 
	 * @param key sysmbolic name
	 * @param kind 
	 * @param label 
	 * @param tooltip 
	 * @param clazz 
	 * 
	 * @return the creationtool Entry
	 */
	public CreationToolEntry getConnectionCreationToolEntry(String label, String tooltip, Class clazz, Enumerator kind){
		String symbolicName=label+tooltip+clazz+kind; 
		CreationToolEntry result= creationToolEntryManager.get(symbolicName);
		if(result==null){
			creationToolEntryManager.put(symbolicName, createConnectionCreationToolEntry(label, tooltip, clazz, kind));
			result= creationToolEntryManager.get(symbolicName);
		}	
		return result;
	}
	
	
	/**
	 * this method is used to obtain the creationToolEntry for edge from the button.
	 * if the creationToolEntry does not exist, it is created
	 * 
	 * @param button defined in the extension point
	 * 
	 * @return the creationToolEntry obtained from the button.
	 * if the creationToolEntry does not exist, it is created
	 */
	public  CreationToolEntry getEdgeCreationEntry (Button button)
	{	
		Class clazz = button.getUMLType();
		String kind = button.getUMLTypeKind();
		
		ImageDescriptor iconDesc = button.getIconDescriptor();
		if (iconDesc == null) {
			Image icon = PapyrusPlugin.UML_LABEL_PROVIDER.getImage(clazz, kind);
			iconDesc = ImageDescriptor.createFromImage(icon);
		}

		
		String symbolicName=button.toString(); 
		CreationToolEntry result= creationToolEntryManager.get(symbolicName);
		if(result==null){
			creationToolEntryManager.put(symbolicName, new ConnectionCreationToolEntry(
					button.getLabel()
					, button.getTooltip()
					, new UMLElementFactory(button)
					, iconDesc
					, iconDesc));
			result= creationToolEntryManager.get(symbolicName);
		}	
		return  result;
		
	}
	
	
	/**
	 * this method is used to obtain the creationToolEntry for nodes from the button.
	 * if the creationToolEntry does not exist, it is created
	 * 
	 * @param button defined in the extension point
	 * 
	 * @return the creationToolEntry obtained from the button.
	 * if the creationToolEntry does not exist, it is created
	 */
	
	public  CreationToolEntry getNodeCreationEntry (Button button)
	{	
		Class clazz = button.getUMLType();
		String kind = button.getUMLTypeKind();
		
		ImageDescriptor iconDesc = button.getIconDescriptor();
		if (iconDesc == null) {
			Image icon = PapyrusPlugin.UML_LABEL_PROVIDER.getImage(clazz, kind);
			iconDesc = ImageDescriptor.createFromImage(icon);
		}

		
		String symbolicName=button.toString(); 
		CreationToolEntry result= creationToolEntryManager.get(symbolicName);
		if(result==null){
			creationToolEntryManager.put(symbolicName, new CombinedTemplateCreationEntry(
					button.getLabel()
					, button.getTooltip()
					, clazz
					, new UMLElementFactory(button)
					, iconDesc
					, iconDesc));
			result= creationToolEntryManager.get(symbolicName);
		}	
		return  result;
		
	}

}